//
//  KLVipViewController.m
//  Meboo
//
//  Created by kyo on 2024/1/2.
//

#import "KLVipViewController.h"
#import "KLPaymentManager.h"
#import "MBVipHeaderView.h"
#import "MBVipPopView.h"
#import "MBWebViewController.h"
#import "KLVipFuncCell.h"
#import "KLVipSubscriptionCell.h"
@interface KLVipViewController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UIImageView *backImageView;
@property(nonatomic,strong) MBVipHeaderView *vipHeaderView;
@property (nonatomic, strong) NSMutableArray *imageNameArr;
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic , strong) MBVipPopView *vipPopView;
@property (nonatomic, strong) NSMutableArray *productIDArr;
@property (nonatomic, copy) NSString *productID;
@property (nonatomic , strong) UIButton *buyBtn;
//恢复购买
@property (nonatomic, strong) UIButton *resumeBtn;

//支付协议
@property (nonatomic, strong) UILabel *privacyLab0;
//会员服务协议
@property (nonatomic, strong) UILabel *privacyLab1;
//续订协议
@property (nonatomic, strong) UILabel *privacyLab2;
@end

@implementation KLVipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXS(@"ffffff");
    [self setupUI];
    [self setVipState];
    //默认值
    self.productID = @"meboo_vip_ios_30_days";
}

#pragma mark - Private Method


#pragma mark - Action

- (void)resumeAction {
    [[KLPaymentManager sharePaymentManager] restorePurchaase];
}

- (void)PurchaseAction {
    __WeakSelf
    self.buyBtn.userInteractionEnabled = NO;
    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
        weakSelf.buyBtn.userInteractionEnabled = YES;
        if (payState) {
            [[MBLoginDataManager sharedManager] getUserInfoWithCode:@"" success:^(BOOL success) {
//                if (success) {
                    [weakSelf setVipState];
//                }
            }];
            weakSelf.vipPopView.hidden = NO;
        }
    };
}

- (void)agreementClick0 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:paymentAgreementWeb]];
    webVC.titleStr = @"PaidPolicy";
    //    webVC.titleStr = @"PrivacyPolicy";
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)agreementClick1 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:MembershipServiceWeb]];
    webVC.titleStr = @"VIP Service Agreement";
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)agreementClick2 {
    //    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    //    [webVC loadWithURL:[NSURL URLWithString:AutoRenewalAgreementWeb]];
    //    webVC.titleStr = @"About Subscription";
    //    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)setVipState {
    
    if ([MBUserAccount defaultAccount].isVip) {
        self.vipHeaderView.dateLab.text = [NSString stringWithFormat:@"Vip expiration time:%@",[MBUserAccount defaultAccount].vipDateStr];
        self.vipHeaderView.vipDateView.hidden = NO;
    } else {
        self.vipHeaderView.vipDateView.hidden = YES;
    }
}

#pragma mark -- collectionView  delegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //    if (indexPath.row < 7) {
    KLVipFuncCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KLVipFuncCell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArr[indexPath.row];
    cell.iconImageView.image = ImageWithName(self.imageNameArr[indexPath.row]);
    return cell;
    //    } else {
    //        KLVipSubscriptionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KLVipSubscriptionCell" forIndexPath:indexPath];
    //        return cell;
    //    }
    
}

//设置每个section的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(MBHeightScale(10), MBWidthScale(0), MBHeightScale(10), MBWidthScale(0));
}

//设置列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return MBHeightScale(15);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return MBWidthScale(0);
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row < 7) {
        return CGSizeMake(ScreenWidth-MBWidthScale(40), MBHeightScale(60));
//    } else {
//        return CGSizeMake(ScreenWidth-MBWidthScale(40), MBHeightScale(480));
//    }
    
}

#pragma mark - UI
- (void)setupUI {
    
    [self.view addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    CGFloat height = (ScreenWidth-MBWidthScale(40))*127/334;
    [self.view addSubview:self.vipHeaderView];
    [self.vipHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(StatusBarAndNavigationBarHeight+MBHeightScale(20)+height+MBHeightScale(148));
    }];
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.vipHeaderView.mas_bottom).offset(MBHeightScale(15));
        make.right.left.equalTo(self.view);
        make.bottom.mas_equalTo(MBWidthScale(-85)-TabbarSafeBottomMargin);
    }];
    
        [self.view addSubview:self.buyBtn];
        [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(MBWidthScale(20));
            make.width.mas_equalTo(ScreenWidth-MBWidthScale(40));
            make.bottom.mas_equalTo(MBWidthScale(-20)-TabbarSafeBottomMargin);
            make.height.mas_equalTo(MBWidthScale(58));
        }];
  
//    [self.view addSubview:self.resumeBtn];
//    [self.resumeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.view.mas_top).offset(StatusBarAndNavigationBarHeight-22);
//        make.right.mas_equalTo(MBWidthScale(-20));
//    }];
    
    [self.view addSubview:self.privacyLab0];
    [self.privacyLab0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buyBtn.mas_bottom).offset(MBHeightScale(2));
        make.left.mas_equalTo(MBWidthScale(20));
    }];
    
    [self.view addSubview:self.privacyLab1];
    [self.privacyLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.privacyLab0);
        make.left.equalTo(self.privacyLab0.mas_right).offset(15);
    }];
    
//    [self.view addSubview:self.privacyLab2];
//    [self.privacyLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.privacyLab0);
//        make.left.equalTo(self.privacyLab1.mas_right).offset(15);
//    }];
    
    //确保在最外层
    [self.view addSubview:self.vipPopView];
    [self.vipPopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - Setter && Getter

- (UIButton *)buyBtn {
    if (!_buyBtn) {
        _buyBtn = [UIButton new];
        [_buyBtn setBackgroundImage:ImageWithName(@"vip_purchase_back_check") forState:0];
        [_buyBtn setTitle:@"$29.9  Purchase" forState:0];
        _buyBtn.titleLabel.font = PDBoldScale(20);
        [_buyBtn setTitleColor:HEXS(@"C07013") forState:0];
        [_buyBtn addTarget:self action:@selector(PurchaseAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buyBtn;
}

- (UIButton *)resumeBtn {
    if (!_resumeBtn) {
        _resumeBtn = [[UIButton alloc] init];
        _resumeBtn.titleLabel.font = MBFontSizeScale(10);
        [_resumeBtn setTitleColor:HEXS(@"ffffff") forState:0];
        [_resumeBtn setTitle:@"restore purchase" forState:0];
        [_resumeBtn addTarget:self action:@selector(resumeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resumeBtn;
}

- (UILabel *)privacyLab0 {
    if (!_privacyLab0) {
        _privacyLab0 = [UILabel new];
        _privacyLab0.userInteractionEnabled = YES;
        _privacyLab0.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PaidPolicy" attributes:attribtDic];
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PrivacyPolicy" attributes:attribtDic];
        
        //赋值
        _privacyLab0.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick0)];
        
        [_privacyLab0 addGestureRecognizer:gestureRecognizer];
    }
    return _privacyLab0;
}

- (UILabel *)privacyLab1 {
    if (!_privacyLab1) {
        _privacyLab1 = [UILabel new];
        _privacyLab1.userInteractionEnabled = YES;
        _privacyLab1.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"VIP Service Agreement" attributes:attribtDic];
        //赋值
        _privacyLab1.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick1)];
        
        [_privacyLab1 addGestureRecognizer:gestureRecognizer];

    }
    return _privacyLab1;
}

- (UILabel *)privacyLab2 {
    if (!_privacyLab2) {
        _privacyLab2 = [UILabel new];
        _privacyLab2.userInteractionEnabled = YES;
        _privacyLab2.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"About Subscription" attributes:attribtDic];
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"restore purchase" attributes:attribtDic];
        //赋值
        _privacyLab2.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick2)];
        
        [_privacyLab2 addGestureRecognizer:gestureRecognizer];

    }
    return _privacyLab2;
}

- (NSMutableArray *)titleArr {
    if (!_titleArr) {
        _titleArr = [NSMutableArray arrayWithArray:@[@"Unlimited Following",@"Unlimited Disinterest",@"Unlimited Contact with Authors",@"VIP Avatar Frame",@"Send Photos in IM Chat Pages",@"Photo Protection",@"IM Chat Records Retained for 7 Days"]];
    }
    return _titleArr;
}

- (NSMutableArray *)imageNameArr {
    if (!_imageNameArr) {
        _imageNameArr = [NSMutableArray arrayWithArray:@[@"vipprofile_vip_func1",@"vipprofile_vip_func2",@"vipprofile_vip_func3",@"vipprofile_vip_func4",@"vipprofile_vip_func5",@"vipprofile_vip_func6",@"vipprofile_vip_func7"]];
    }
    return _imageNameArr;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView.hidden = YES;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[KLVipFuncCell class] forCellWithReuseIdentifier:@"KLVipFuncCell"];
        [_collectionView registerClass:[KLVipSubscriptionCell class] forCellWithReuseIdentifier:@"KLVipSubscriptionCell"];
    }
    return _collectionView;
}

- (MBVipHeaderView *)vipHeaderView {
    if (!_vipHeaderView) {
        __WeakSelf
        _vipHeaderView = [MBVipHeaderView new];
        _vipHeaderView.selectBlock = ^(NSInteger tag, NSString * _Nonnull priceStr) {
            [weakSelf.buyBtn setTitle:[NSString stringWithFormat:@"%@  Purchase",priceStr] forState:0];
            weakSelf.productID = weakSelf.productIDArr[tag];
        };
    }
    return _vipHeaderView;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.image = ImageWithName(@"vipprofile_back_image");
    }
    return _backImageView;
}

- (MBVipPopView *)vipPopView {
    if (!_vipPopView) {
        _vipPopView = [MBVipPopView new];
        _vipPopView.hidden = YES;
    }
    return _vipPopView;
}

- (NSMutableArray *)productIDArr {
    if (!_productIDArr) {
        _productIDArr = [NSMutableArray arrayWithArray:@[@"meboo_vip_ios_90_days",@"meboo_vip_ios_30_days",@"meboo_vip_ios_7_days"]];
    }
    return _productIDArr;
}
@end
